package com.wzqj.agent.web.controller;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wzqj.agent.common.ConfigHelper;
import com.wzqj.agent.util.CreateQRCodeKit;

@Controller
@RequestMapping("/qrcode")
public class QRCodeController {
	
	/**
	 * 二维码高度
	 */
	private static final int HEIGHT = 300;
	
	/**
	 * 二维码宽度
	 */
	private static final int WIDTH = 300;
	/**
	 * 二维码图片保存格式 png / jpg / bmp
	 */
	private static final String FORMAT = "png";
	/**
	 * 二维码内容（参数），此处为url
	 */
	private static final String URL = "http://"+ConfigHelper.getProperty("domain")+"/agent/qrcode/downloadAPK.xhtml";
	
	
	/**
	 * 扫码跳转到下载APK页面
	 * @return
	 */
	@RequestMapping("/downloadAPK")
	public String toDownLoadPage(){
		return "/user/common/downloadAPK";
	}
	/**
	 * 生成二维码
	 * @param response
	 */
	@RequestMapping("/show")
	public void show(HttpServletRequest request,HttpServletResponse response) {
		 OutputStream out = null;
		    try {
		        byte[] qr = CreateQRCodeKit.createQr(URL, HEIGHT, WIDTH, FORMAT).toByteArray();
		    out = response.getOutputStream();
			out.write(qr);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
		    try {
		        if (out != null){
		            out.close();
		        }
		    } catch (IOException e) {
		        out = null;
		    }
		}

	}

}
